package cn.cvs.controller;

import cn.cvs.entity.SysRole;
import cn.cvs.entity.SysUser;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.entity.SysRole;
import cn.cvs.entity.SysUser;
import cn.cvs.utils.Constants;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

//import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 角色模块控制器
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

//    Logger logger = Logger.getLogger(SysRoleController.class);
//    @Resource
//    private SysRoleService sysRoleService;

    /**
     * 查询角色列表
     * @param model
     * @return
     */
    @GetMapping(value="/list")
    public String getRoleList(Model model)throws Exception{
        List<cn.cvs.entity.SysRole> roleList = sysRoleService.getRoleList();
        model.addAttribute("roleList", roleList);
        return "sysRole/list";
    }

    /**
     * 查询简单角色列表
     * @return
     */
    @GetMapping(value = "/simpleList",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object getRoleList()throws Exception{
        List<cn.cvs.entity.SysRole> roleList = sysRoleService.getRoleList();
        return JSONArray.toJSONString(roleList);
    }

    /**
     * 跳转道添加角色页面
     * @param sysRole
     * @return
     */
//    @GetMapping(value="/toAdd")
//    public String addRole(@ModelAttribute("role") SysRole sysRole){
//        return "sysRole/add";
//    }

    /**
     * 添加角色
     * @param sysRole
     * @param session
     * @return
     */
//    @PostMapping(value="/add")
//    public String addRoleSave(SysRole sysRole,HttpSession session)throws Exception{
//        sysRole.setCreatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
//        if(sysRoleService.add(sysRole)){
//            return "redirect:/sys/role/list";
//        }
//        return "sysRole/add";
//    }

    /**
     * 跳转到修改角色页面
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String getUserById(@PathVariable Integer id,Model model,HttpServletRequest request) throws Exception {
        cn.cvs.entity.SysRole sysRole = sysRoleService.getById(id);
        model.addAttribute(sysRole);
        return "sysRole/update";
    }

    /**
     * 修改角色
     * @param sysRole
     * @param session
     * @return
     */
    @PostMapping(value = "/update")
    public String update(SysRole sysRole, HttpSession session) throws Exception {
        Object sessionUserObj = session.getAttribute(Constants.USER_SESSION);
        if (sessionUserObj != null && sessionUserObj instanceof SysUser) {
            SysUser sessionUser = (SysUser) sessionUserObj;
            // 假设这里是获取用户相关信息做合理关联，而非错误赋值给角色名
            // 比如可能是设置更新人ID等合理操作
            sysRole.setUpdatedBy(sessionUser.getId());
        }
        cn.cvs.entity.SysRole SysRole = null;
        if (sysRoleService.update(SysRole)) {
            return "redirect:/sys/role/list";
        }
        return "sysRole/update";
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
//    @DeleteMapping(value="/del/{id}")
//    @ResponseBody
//    public Object del(@PathVariable(value = "id", required = true) Integer id){
//        HashMap<String, String> resultMap = new HashMap<String, String>();
//        try {
//            if(sysRoleService.deleteById(id)){
//                resultMap.put("delResult", "true");
//            }
//            else{
//                resultMap.put("delResult", "false");
//            }
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return JSONArray.toJSONString(resultMap);
//    }


    /**
     * 查询角色编号是否已存储在
     * @param code
     * @return
     */
    @RequestMapping(value="/codeExist")
    @ResponseBody
    public Object odeIsExit(@RequestParam(required = true) String code)throws Exception{
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        // 角色编号不可为空
        if(code == null || "".equals(code.trim())){
            resultMap.put("exist", -1);
            resultMap.put("msg", "角色编号不可以为空");
        }else {
            int count = sysRoleService.getCountByCode(code);
            if(count == 0){
                resultMap.put("exist", 0);
            }
            else{
                resultMap.put("exist", 1);
            }
        }
        return JSONArray.toJSONString(resultMap);
    }
    public class SysRole {
        private String roleCode;
        private String roleName;
        // 可按需添加其他属性，如创建时间等
        // 以及对应的getter和setter方法
        public String getRoleCode() {
            return roleCode;
        }
        public void setRoleCode(String roleCode) {
            this.roleCode = roleCode;
        }
        public String getRoleName() {
            return roleName;
        }
        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }

        public void setUpdatedBy(Integer id) {

        }
    }
    /**
     * 角色管理
     */
    @Autowired
    private SysRoleService sysRoleService;
    @Controller
    public class RoleController {

        @Autowired
        private SysRoleService sysRoleService;

        @GetMapping("/role/list")
        public String getRoleList(Model model) {
            List<cn.cvs.entity.SysRole> roleList = null;
            try {
                roleList = sysRoleService.getRoleList();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            model.addAttribute("roleList", roleList);
            return "roleList";
        }
    }
}
